<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/26
 * Time: 10:42
 */

namespace app\financial\controller;


use app\financial\model\HelpAwardModel;
use app\financial\model\HelpAwardMoneyModel;
use cmf\controller\AdminBaseController;
use think\Db;

class HelpAwardController extends  AdminBaseController
{
    public function index()
    {

        $param=$this->request->param();
        $help_model=new HelpAwardModel();
        $data=$help_model->getList($param);

        $help_money_model=new HelpAwardMoneyModel();
        $help_money_data=$help_money_model->select()->toArray();

        $this->assign('data',$data);
        $this->assign('page',$data->render());
        $this->assign('param',$param);
        $this->assign('help_money_data',$help_money_data);

        return $this->fetch();
    }
    public function  editPost()
    {
        $param=$this->request->param();
        $id=$param['id'];
        $status=$param['status'];
        $help_model=new HelpAwardModel();
        $help_award_money=new HelpAwardMoneyModel();
        $pid=cmf_get_current_admin_id();
        $res=$help_model->where('id',$id)->update(['status'=>$param['status'],'u_id'=>$pid]);
        $student_id=$help_model->where('id',$id)->value('student_id');
        $type=$help_model->where('id',$id)->value('award_id');
        $price=$help_award_money->where(['id'=>$type])->value('price');

        $help_model->where(['id'=>$id])->update(['update_time'=>time()]);
        if($status==2)
        {
            $help_award_model_data=$help_model->where(['student_id'=>$student_id,'status'=>2])->select()->toArray();
            if(count($help_award_model_data)>1)
            {
                foreach($help_award_model_data as $k=>$v)
                {
                    if($price>$v['money']){
                        $help_model->where(['id'=>$v['id']])->update(['delete_time'=>time()]);
                    }else if($price<$v['money']){
                        $help_model->where(['id'=>$id])->update(['delete_time'=>time()]);
                    }
                }
            }
        }
        if($res)
        {
            //记录日志

            $pk = $id; //主键
            $table = 'cmf_help_award';//数据表名
            $pid=cmf_get_current_admin_id();
            $user_login=Db::name('user')->where(['id'=>$pid])->value('user_login');
            $content =$user_login.'批准了助学金';
            do_log($content, $pk, $table, time());
            $this->success('操作成功');
        }
        $this->error('操作失败');
    }
    public function editZhuPost()
    {
        $help_money_model=new HelpAwardMoneyModel();
        $param=$this->request->param();
        $type=intval($param['type']);


        if($type==0){
            $this->error('请选择助学金级别');
        }
        if($type==1)
        {
            if($param['award_money1']<0)
            {
                $this->error('助学金为负值');
            }
            $res=$help_money_model->where('type',$type)->update(['price'=>$param['award_money1']]);
        }
        if($type==2)
        {

            if($param['award_money2']<0)
            {
                $this->error('助学金为负值');
            }
            $res=$help_money_model->where('type',$type)->update(['price'=>$param['award_money2']]);
        }
        if($type==3)
        {

            if($param['award_money3']<0)
            {
                $this->error('助学金为负值');
            }
            $res=$help_money_model->where('type',$type)->update(['price'=>$param['award_money3']]);
        }

       if($res)
       {
           $this->success('修改成功');
       }
        $this->error('修改失败');
    }
    /*
     * 下载
     */
    public function  down()
    {


        $asset_id=$this->request->param('asset_id');
        $filepath=Db::name('asset')->where('id',$asset_id)->value('file_path');
        $filename=Db::name('asset')->where('id',$asset_id)->value('filename');

        $path=$_SERVER['DOCUMENT_ROOT']."/upload/".$filepath;
//         $path=$_SERVER['HTTP_HOST']."/upload/".$filepath;


        //实现下载这个功能代码
        header('content-disposition:attachment;filename='.$filename);

//        dump(__FILE__);
//        dump(basename(__FILE__));
//        exit();
//        header('content-disposition:attachment;filename='.basename(__FILE__));
        //要把下载文件里面的内容打印到这个页面
         readfile($path);
    }
}